package com.cjz.roomdbdemo.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.LiveData;
import android.support.annotation.NonNull;

import com.cjz.roomdbdemo.db.Note;
import com.cjz.roomdbdemo.db.NoteRepository;

import java.util.List;

/**
 * NoteViewModel 只与 NoteRepository 打交道，注意此时仍在主线程
 * UI层 通过 NoteRepository 与数据库逻辑隔离
 * <p>
 * <p>
 * Created by cjz on 2018/8/31.
 */
public class NoteViewModel extends AndroidViewModel {

    private NoteRepository repository;

    private LiveData<List<Note>> allNotes;

    public NoteViewModel(@NonNull Application application) {
        super(application);
        repository = new NoteRepository(application);
        allNotes = repository.getAllNotes();
    }

    public void insert(Note note) {
        repository.insert(note);
    }

    public LiveData<List<Note>> getAllNotes() {
        return allNotes;
    }

    public void deleteAll() {
        repository.deleteAll();
    }
}
